package com.utils.redis.utils;

import com.google.common.base.Splitter;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import redis.clients.jedis.Jedis;

import java.util.Properties;
import java.util.Set;

import static com.utils.redis.common.PropertyConstants.*;

/**
 * @author xin.ding
 * @date 2020/9/22 16:13
 */
@Slf4j
public class JedisSimpleUtils {

    private static Splitter commaSplitter = Splitter.on(",").omitEmptyStrings().trimResults();


    private static String host;
    private static int port;
    private static String password;

    private static Jedis jedis;

    private JedisSimpleUtils() {
        throw new UnsupportedOperationException();
    }

    /**
     * 初始化配置参数
     */
    private static void initParam() {
        Properties conf = new Properties();
//        try (InputStream input = Resources.asByteSource(Resources.getResource(PropertyConstants.CONFIG_PROPERTY_NAME)).openStream()) {
//            conf.load(input);
//        } catch (Exception e) {
//            log.error("failed to load {}, cause by:{}", CONFIG_PROPERTY_NAME, Throwables.getStackTraceAsString(e));
//        }

        try {
            conf.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_PROPERTY_NAME));
        } catch (Exception e) {
            log.error("failed to load {}, cause by:{}", CONFIG_PROPERTY_NAME, e.getMessage());
        }

        Set<String> serviceNames = Sets.newHashSet();
        String services = conf.getProperty(PKEY_SERVICES);
        if (StringUtils.isNotEmpty(services)) {
            log.info("get configuration[service.names] -> {}", services);
            serviceNames.addAll(commaSplitter.splitToList(services));
        } else {
            serviceNames.add(DEFAULT_SERVICE_KEY);
            log.info("load default configuration[serviceName] -> {}", DEFAULT_SERVICE_KEY);
        }

        for (String serviceName : serviceNames) {
            String format = String.format(PKEY_IP, serviceName);
            String configHost = conf.getProperty(format);
            if (StringUtils.isNotEmpty(configHost)) {
                log.info("set configuration[{}] -> {}", format, configHost);
                host = configHost;
            }

            format = String.format(PKEY_PORT, serviceName);
            String configPort = conf.getProperty(format);
            if (StringUtils.isNotEmpty(configPort)) {
                log.info("set configuration[{}] -> {}", format, configPort);
                port = Integer.parseInt(configPort);
            }

            format = String.format(PKEY_PASSWORD, serviceName);
            String configPassword = conf.getProperty(format);
            if (StringUtils.isNotEmpty(configPassword)) {
                log.info("set configuration[{}] -> {}", format, configPassword);
                password = configPassword;
            }
        }
    }

    /**
     * 获取Jedis 客户端
     *
     * @return Jedis
     */
    public static Jedis getJedisClient() {
        if (jedis == null) {
            initParam();
            jedis = new Jedis(host, port);
            if (StringUtils.isNotEmpty(password)) {
                jedis.auth(password);
            }
        }
        return jedis;
    }

}
